/**
 * Copyright 2008-2009 HAN.BIFANG
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package han.bifang.business.sec.rbac.db;

import han.bifang.business.dao.ResourceDao;
import han.bifang.business.dao.RoleDao;
import han.bifang.business.dao.RoleResourceRelDao;
import han.bifang.business.sec.rbac.ResourceProvider;
import han.bifang.business.sync.SyncUtils;
import han.bifang.common.entity.SoaResource;
import han.bifang.common.entity.SoaRole;
import han.bifang.common.entity.SoaRoleResRel;
import han.bifang.common.entity.SoaRoleResRelPK;
import han.bifang.common.exception.BifangException;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author Kent
 */
public class DbResourceProvider implements ResourceProvider {

	@Override
	public void addRolesToResource(String[] roleIds, String resourceId)
			throws BifangException {
		ArrayList<SoaRoleResRel> r = new ArrayList<SoaRoleResRel>();
		for (String id : roleIds) {
			SoaRoleResRel s = new SoaRoleResRel();
			SoaRoleResRelPK sk = new SoaRoleResRelPK();
			sk.setResId(resourceId);
			sk.setRoleId(id);
			s.setCompId(sk);
			r.add(s);
		}
		RoleResourceRelDao.createRoleResRel(r);
		SyncUtils.sync(SyncUtils.SECURITY, null);
	}

	@Override
	public String createResource(SoaResource r) throws BifangException {
		return ResourceDao.createResource(r);
	}

	@Override
	public SoaResource getResourceById(String id) throws BifangException {
		return ResourceDao.getResource(id);
	}

	@Override
	public SoaResource getResourceByEntity(String entityId, String entityType)
			throws BifangException {
		return ResourceDao.getResourceByEntity(entityId, entityType);
	}

	@Override
	public List<SoaRole> getRolesOfResource(String resourceId)
			throws BifangException {
		return RoleDao.findRoleByResId(resourceId);
	}


	@Override
	public void modifyResource(SoaResource r) throws BifangException {
		ResourceDao.modifyResource(r);
		SyncUtils.sync(SyncUtils.SECURITY, null);

	}

	@Override
	public void removeRolesFromResource(String[] roleIds, String resourceId)
			throws BifangException {
		ArrayList<SoaRoleResRel> r = new ArrayList<SoaRoleResRel>();
		for (String id : roleIds) {
			SoaRoleResRel s = new SoaRoleResRel();
			SoaRoleResRelPK sk = new SoaRoleResRelPK();
			sk.setResId(resourceId);
			sk.setRoleId(id);
			s.setCompId(sk);
			r.add(s);
		}
		RoleResourceRelDao.removeRoleResRel(r);
		SyncUtils.sync(SyncUtils.SECURITY, null);

	}

	@Override
	public void removeResourceByEntity(String entityId, String entityType)
			throws BifangException {
		SoaResource r = ResourceDao.getResourceByEntity(entityType, entityId);
		ResourceDao.removeResourceByEntity(entityType, entityId);
		RoleResourceRelDao.removeRoleResRelByResId(r.getId());
		SyncUtils.sync(SyncUtils.SECURITY, null);

	}

	@Override
	public void removeResourceById(String resourceId) throws BifangException {
		ResourceDao.removeResource(resourceId);
		RoleResourceRelDao.removeRoleResRelByResId(resourceId);
		SyncUtils.sync(SyncUtils.SECURITY, null);
	}

	@Override
	public List getRolesNotOfResourceByPage(String resourceId, String roleName,
			int page, int pageSize) throws BifangException {
		return RoleDao.findRolesNotOfResource(resourceId, roleName, page, pageSize);
	}

}
